import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:platform_version/platform_version.dart';
import 'package:random_number_plugin/random_number_plugin.dart';
import 'package:show_my_toast/show_my_toast.dart';

class DartPluginDemoPage extends StatefulWidget{

//  @override
//  State<StatefulWidget> createState() {
//
//    return new FirstPageState();
//  }

  @override
  DartPluginState createState() => DartPluginState();

}

class DartPluginState extends State<DartPluginDemoPage>{

  String _versions = "-1";
  int _random = -1;

  int count = 0;

  //Future 将来的意思
  // Future表示在将来某时获取一个值的方式。当一个返回Future的函数被调用的时候，做了两件事情：
  //1. 函数把自己放入队列和返回一个未完成的Future对象
  //2. 之后当值可用时，Future带着值变成完成状态。
  //为了获得Future的值，有两种方式：
  //1. 使用async和await
  //2. 使用Future的接口

  //   Future<void> 可以不写，也可以写一个返回值
  Future<void> getPlatformVersion() async {

    String version = await PlatformVersion.platformVersion;

    setState(() {

      count = count +1;
      _versions = version;

    });

  }

  Future<void> getRandomNumber() async {

    int random = await RandomNumberPlugin.getRandomNumber();

    setState(() {

      _random = random;

    });
  }

  //  Future<void> 也可以不写，该示例来至于创建插件时的ide自动生成的example的调用方式。
  Future<void> getRandomNumber1() async{

    int random;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      random = await RandomNumberPlugin.randomNumber;
    } on PlatformException {
      random = -1;
    }

    if (!mounted) return;

    setState(() {
      _random = random;
    });

  }

  void showLongToast() {
    ShowMyToast.showToast(
      msg: "This is Long Toast",
      toastLength: Toast.LENGTH_LONG,
    );
  }

  void showColoredToast() {
    ShowMyToast.showToast(
        msg: "This is Colored Toast with android duration of 5 Sec",
        toastLength: Toast.LENGTH_SHORT,
        backgroundColor: Colors.red,
        textColor: Colors.white);
  }

  void showShortToast() {
    ShowMyToast.showToast(
        msg: "This is Short Toast",
        toastLength: Toast.LENGTH_SHORT,
        timeInSecForIos: 1);
  }

  void showTopShortToast() {
    ShowMyToast.showToast(
        msg: "This is Top Short Toast",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.TOP,
        timeInSecForIos: 1);
  }

  void showCenterShortToast() {
    ShowMyToast.showToast(
        msg: "This is Center Short Toast",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIos: 1);
  }

  void cancelToast() {
    ShowMyToast.cancel();
  }

  @override
  void initState() {
    super.initState();
    //初始化，这个函数在生命周期中只调用一次

    print("initState执行");

  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();

    //在initState之后调 Called when a dependency of this [State] object changes.
    print("didChangeDependencies执行");

  }

  @override
  void dispose() {
    super.dispose();
    //销毁
    print("dispose执行");
  }


  @override
  void didUpdateWidget(DartPluginDemoPage oldWidget) {
    super.didUpdateWidget(oldWidget);

    print("didUpdateWidget执行");

  }

  @override
  Widget build(BuildContext context) {

    print("build执行");

    return new Scaffold(

      appBar: new AppBar(

        title: new Text('dartPlugin开发demo'),
        backgroundColor: Colors.orange[300],

      ),
      body: new SingleChildScrollView(

        child: new Column(

          children: <Widget>[

            new Padding(
              padding: new EdgeInsets.fromLTRB(10.0, 20.0, 10.0, 20.0),
              child: new Row(
                children: <Widget>[
                  new Expanded(

                    child: new Container(
                      padding: new EdgeInsets.fromLTRB(10, 10, 10, 10),
                      margin: new EdgeInsets.fromLTRB(10, 10, 10, 10),
                      color: Colors.blue[50],
                      height: 100,
                      child: new Text("当前的系统版本为:$_versions，获取次数$count"
                        ,style: new TextStyle(

                          fontSize: 12,
                          color: Colors.red,

                        ),),

                    ),
                  ),

                ],


              ),
            ),

            new Padding(
              padding: new EdgeInsets.fromLTRB(10.0, 20.0, 10.0, 20.0),
              child: new Row(
                children: <Widget>[
                  new Expanded(

                    child: new Container(
                      padding: new EdgeInsets.fromLTRB(10, 10, 10, 10),
                      margin: new EdgeInsets.fromLTRB(10, 10, 10, 10),
                      color: Colors.blue[50],
                      height: 100,
                      child: new Text("获取到的随机数:$_random"
                        ,style: new TextStyle(

                          fontSize: 12,
                          color: Colors.red,

                        ),),

                    ),
                  ),

                ],


              ),
            ),

            new Padding(
              padding: new EdgeInsets.fromLTRB(10.0, 20.0, 10.0, 20.0),
              child: new Row(
                children: <Widget>[
                  new Expanded(

                    child: new Container(
                      padding: new EdgeInsets.fromLTRB(10, 10, 10, 10),
                      margin: new EdgeInsets.fromLTRB(10, 10, 10, 10),
                      color: Colors.blue[500],
                      height: 100,
                      child: new RaisedButton(onPressed: (){

                        getPlatformVersion();

                      },
                      child: new Text("点击获取系统版本"),
                      ),

                    ),
                  ),

                ],


              ),
            ),

            new Padding(
              padding: new EdgeInsets.fromLTRB(10.0, 20.0, 10.0, 20.0),
              child: new Row(
                children: <Widget>[
                  new Expanded(

                    child: new Container(
                      padding: new EdgeInsets.fromLTRB(10, 10, 10, 10),
                      margin: new EdgeInsets.fromLTRB(10, 10, 10, 10),
                      color: Colors.blue[500],
                      height: 100,
                      child: new RaisedButton(onPressed: (){

                        getRandomNumber();

                      },
                        child: new Text("点击获取随机数"),
                      ),

                    ),
                  ),

                ],


              ),
            ),

            new Padding(
              padding: new EdgeInsets.fromLTRB(10.0, 20.0, 10.0, 20.0),
              child: new Row(
                children: <Widget>[
                  new Expanded(

                    child: new Container(
                      padding: new EdgeInsets.fromLTRB(10, 10, 10, 10),
                      margin: new EdgeInsets.fromLTRB(10, 10, 10, 10),
                      color: Colors.blue[500],
                      height: 100,
                      child: new RaisedButton(onPressed: (){

                        showColoredToast();

                      },
                        child: new Text("弹出我的自定义Toast"),
                      ),

                    ),
                  ),

                ],


              ),
            ),


          ],



        ),

      ),

    );
  }

}